/**********************************************
 * 
 * File Name				:	MenuAction.java
 * 
 * Last Changed Date		: 	$Date: 2009-08-06 14:51:00 +0800 (2009) $
 * 
 * Last Changed Revision	:	$Revision: 92 $
 * 
 * Last Changed Author		:	$Author: cfanboy $
 * 
 * File Header URL			:	$HeadURL: svn://www.svnhost.cn/Busi-Building/code/branch/src/com/busibuilding/administrate/menu/action/MenuAction.java $
 * 
 * ********************************************
 */

package com.busibuilding.administrate.menu.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.displaytag.tags.TableTagParameters;

import com.busibuilding.administrate.menu.services.MenuService;
import com.busibuilding.administrate.menu.vo.MenuCVO;
import com.busibuilding.codemstr.services.CodeMstrService;
import com.busibuilding.codemstr.vo.CodeMstrCVO;
import com.busibuilding.common.action.CommonActionImpl;
import com.busibuilding.common.displaytag.PaginatedListHelper;
import com.busibuilding.common.exceptions.ServiceException;
import com.busibuilding.utils.StringUtil;

/**
 * Class Name: MenuAction.java 
 * Description: 
 * @author YU Zhipeng
 * @version 1.0
 * @since 1.0 Date: 2009-7-26
 */

public class MenuAction extends CommonActionImpl {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2707551032727455538L;

	private MenuService service;
	private CodeMstrService codeMstrService;

	public void setService(MenuService service) {
		this.service = service;
	}

	public void setCodeMstrService(CodeMstrService codeMstrService) {
		this.codeMstrService = codeMstrService;
	}

	public String input() throws Exception {
		return INPUT;
	}

	public String inputCreateMenu() throws Exception {
		showAddPage = true;
		showTable = false;
		showUpdatePage = false;
		clearAddOrUpdateForm();
		return INPUT;
	}

	public String inputUpdateMenu() throws Exception {
		showAddPage = false;
		showUpdatePage = true;
		research = true;
		runSearch();
		
		String id = getRequest().getParameter("id");
		if (StringUtil.isNotEmpty(id)){
			MenuCVO param = new MenuCVO();
			param.setMenuId(Integer.parseInt(id));
			List<MenuCVO> resultList = service.searchMenu(param);
			if (resultList.size() == 1){
				MenuCVO cvo = resultList.get(0);
				addOrUpdateForm.setMenuId(cvo.getMenuId().toString());
				this.setMenuId(cvo.getMenuId());
				addOrUpdateForm.setMenuCode(cvo.getMenuCd());
				addOrUpdateForm.setMenuName(cvo.getMenuName());
				addOrUpdateForm.setMenuType(cvo.getMenuType());
				addOrUpdateForm.setMenuUrl(cvo.getMenuUrl());
				addOrUpdateForm.setMenuDesc(cvo.getMenuDesc());
				addOrUpdateForm.setMenuParentId(cvo.getParentMenuId() == null ? "" : cvo.getParentMenuId().toString());
				version = cvo.getVersion();
			}
		}
		return INPUT;
	}
	
	
	public String updateMenu() throws Exception{
		if (hasErrors()){
			return INPUT;
		}
		
		MenuCVO menu = new MenuCVO();
		if (StringUtil.isNotEmpty(addOrUpdateForm.getMenuId())){
			menu.setMenuId(Integer.parseInt(addOrUpdateForm.getMenuId()));
		}else{
			txnFailure(TRANS_FAILURE_MSG);
			return INPUT;
		}
		
		menu.setMenuCd(addOrUpdateForm.getMenuCode());
		menu.setMenuName(addOrUpdateForm.getMenuName());
		menu.setMenuType(addOrUpdateForm.getMenuType());
		menu.setMenuUrl(addOrUpdateForm.getMenuUrl());
		menu.setMenuDesc(addOrUpdateForm.getMenuDesc());
		menu.setParentMenuId("".equals(addOrUpdateForm.getMenuParentId()) ? null : Integer.parseInt(addOrUpdateForm.getMenuParentId()));
		menu.setDefunctInd(NO);
		menu.setVersion(version);
		getAdminCommonInformationForUpdate(menu);
		service.updateMenu(menu);
		txnSuccessful(TRANS_SUCCESSFUL_MSG);
		research = true;
		showUpdatePage = true;
		runSearch();
		return INPUT;		
	}

	public String createMenu() throws Exception {
		if (hasErrors()) {
			return INPUT;
		}
		MenuCVO paramCd = new MenuCVO();
		paramCd.setMenuCd(addOrUpdateForm.getMenuCode());
		List<MenuCVO> list = service.searchMenu(paramCd);
		if (list.size() > 0) {
			addActionError(getText("general.message.rectifyInRed"));
			addFieldError("addOrUpdateForm.menuCode", getText(
					"error.admin.sysmgmt.menu.existMenuCode",
					new String[] { addOrUpdateForm.getMenuCode() }));
			return INPUT;
		}

		MenuCVO paramName = new MenuCVO();
		paramName.setMenuName(addOrUpdateForm.getMenuName());
		List<MenuCVO> nameList = service.searchMenu(paramName);
		if (nameList.size() > 0) {
			addActionError(getText("general.message.rectifyInRed"));
			addFieldError("addOrUpdateForm.menuName", getText(
					"error.admin.sysmgmt.menu.existMenuName",
					new String[] { addOrUpdateForm.getMenuName() }));
			return INPUT;
		}

		MenuCVO menu = new MenuCVO();
		menu.setMenuName(addOrUpdateForm.getMenuName());
		menu.setMenuCd(addOrUpdateForm.getMenuCode());
		menu.setMenuType(addOrUpdateForm.getMenuType());
		menu.setMenuDesc(addOrUpdateForm.getMenuDesc());
		menu.setMenuUrl(addOrUpdateForm.getMenuUrl());
		menu
				.setParentMenuId(!"".equals(addOrUpdateForm.getMenuParentId()) ? Integer
						.parseInt(addOrUpdateForm.getMenuParentId())
						: null);
		getAdminCommonInformationForCreate(menu);
		service.createMenu(menu);

		txnSuccessful(TRANS_SUCCESSFUL_MSG);
		clearAddOrUpdateForm();

		return INPUT;
	}

	public String searchMenu() throws Exception {
		if (hasErrors()) {
			return INPUT;
		}
		
		runSearch();

		return INPUT;
	}


	
	private void runSearch() throws ServiceException {
		MenuCVO searchVo = null;
		
		if (research) {
			searchVo = (MenuCVO)getSessionAttribute("SearchCriteria");
		} else {
			searchVo = new MenuCVO();
			if (StringUtil.isNotEmpty(searchForm.getMenuCode())) {
				searchVo.setMenuCd(searchForm.getMenuCode());
			}

			if (StringUtils.isNotEmpty(searchForm.getMenuName())) {
				searchVo.setMenuName(searchForm.getMenuName());
			}

			if (StringUtils.isNotEmpty(searchForm.getMenuType())) {
				searchVo.setMenuType(searchForm.getMenuType());
			}

			if (StringUtils.isNotEmpty(searchForm.getMenuCategory())) {
				searchVo.setParentMenuId(Integer.parseInt(searchForm
						.getMenuCategory()));
			}

			setSessionAttribute("SearchCriteria", searchVo);
		}
        
//		String paramName = new ParamEncoder("displaylist").encodeParameterName(TableTagParameters.PARAMETER_SORT);
//		Object obj = getRequest().getAttribute(paramName);
//		System.out.println("Obj = " + obj);
//		
        searchVo.setMinIndex((getPage() - 1) * DEFAULT_PAGE_SIZE);
        searchVo.setMaxIndex(getPage() * DEFAULT_PAGE_SIZE);
        
        int listSize = service.getRecordsCount(searchVo);
        
        List<MenuCVO> resultList = null;
        if (listSize > 0){
        	resultList = service.searchMenuByPagination(searchVo);
        	for (MenuCVO cvo : resultList) {
    			cvo
    					.setMenuTypeDisplay(MENU_TYPE_FOLDER.equals(cvo
    							.getMenuType()) ? getText("admin.sysmgmt.menu.label.menuParent")
    							: getText("admin.sysmgmt.menu.label.menuItem"));
    		}
        }
        
		
        paging(resultList, listSize, "menuDisplayList");
        showTable = true;		
	}
	
	private void paging(List<MenuCVO> resultList, int listSize, String displaytagName) {
		PaginatedListHelper paginaredList = new PaginatedListHelper();
		paginaredList.setPageNumber(getPage());
		paginaredList.setList(resultList);
		paginaredList.setObjectsPerPage(DEFAULT_PAGE_SIZE);
		paginaredList.setFullListSize(listSize);
		setCurrentPage(paginaredList.getPageNumber());
		getRequest().setAttribute("fullListSize", listSize);
		getRequest().setAttribute(displaytagName, paginaredList);
	}

	public String clear() throws Exception {
		showAddPage = false;
		showTable = false;
		return input();
	}

	@Override
	public void prepare() throws Exception {
		initPage();
	}

	private void initPage() throws Exception {
		CodeMstrCVO param = new CodeMstrCVO();
		param.setCodeCategory("MENU_TYPE");
		setMenuTypeList(codeMstrService.searchCodemstrByCondition(param));

		MenuCVO menuParam = new MenuCVO();
		menuParam.setMenuType(MENU_TYPE_FOLDER);
		setParentMenuList(service.searchMenu(menuParam));

	}

	private void clearAddOrUpdateForm() {
		addOrUpdateForm.setMenuId(EMPUT_STRING);
		addOrUpdateForm.setMenuCode(EMPUT_STRING);
		addOrUpdateForm.setMenuName(EMPUT_STRING);
		addOrUpdateForm.setMenuDesc(EMPUT_STRING);
		addOrUpdateForm.setMenuType(EMPUT_STRING);
		addOrUpdateForm.setMenuUrl(EMPUT_STRING);
		addOrUpdateForm.setMenuParentId(EMPUT_STRING);
		addOrUpdateForm.setMenuCategory(EMPUT_STRING);		
	}

	// private void clearSearchForm(){
	// searchForm.setMenuCode(EMPUT_STRING);
	// searchForm.setMenuName(EMPUT_STRING);
	// searchForm.setMenuType(EMPUT_STRING);
	// }

	private List<CodeMstrCVO> menuTypeList;
	private List<MenuCVO> parentMenuList;

	private MenuForm searchForm = new MenuForm();
	private MenuForm addOrUpdateForm = new MenuForm();
	private boolean showTable;
	private boolean showAddPage;
	private boolean showUpdatePage;
	private boolean research;
	private Integer version;
	private int currentPage;
	private int page;
	private int menuId;

	public int getMenuId() {
		return menuId;
	}

	public void setMenuId(int menuId) {
		this.menuId = menuId;
	}

	public int getPage() {
		if (getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE) == null || "".equals(getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE))) {
        	page = currentPage;
        }else{
        	page = Integer.parseInt(getRequest().getParameter(TableTagParameters.SORT_AMOUNT_PAGE));
        }
		
		if (page == 0){
			page = DEFAULT_PAGE;
		}
		
		return page;
	}

	public void setPage(int page) {
		if (page > 0){
			this.page = page;
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public boolean isResearch() {
		return research;
	}

	public void setResearch(boolean research) {
		this.research = research;
	}

	public boolean isShowUpdatePage() {
		return showUpdatePage;
	}

	public void setShowUpdatePage(boolean showUpdatePage) {
		this.showUpdatePage = showUpdatePage;
	}

	public MenuForm getSearchForm() {
		return searchForm;
	}

	public void setSearchForm(MenuForm searchForm) {
		this.searchForm = searchForm;
	}

	public MenuForm getAddOrUpdateForm() {
		return addOrUpdateForm;
	}

	public void setAddOrUpdateForm(MenuForm addOrUpdateForm) {
		this.addOrUpdateForm = addOrUpdateForm;
	}

	public boolean isShowAddPage() {
		return showAddPage;
	}

	public void setShowAddPage(boolean showAddPage) {
		this.showAddPage = showAddPage;
	}

	public List<MenuCVO> getParentMenuList() {
		return parentMenuList;
	}

	public void setParentMenuList(List<MenuCVO> parentMenuList) {
		this.parentMenuList = parentMenuList;
	}

	public List<CodeMstrCVO> getMenuTypeList() {
		return menuTypeList;
	}

	public void setMenuTypeList(List<CodeMstrCVO> menuTypeList) {
		this.menuTypeList = menuTypeList;
	}

	public boolean isShowTable() {
		return showTable;
	}

	public void setShowTable(boolean showTable) {
		this.showTable = showTable;
	}

	public MenuService getService() {
		return service;
	}

}
